package com.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.INoticeService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("notice")
public class NoticeController {

	@Autowired
	private INoticeService noticeService;
	
	@RequestMapping("")
	public ModelAndView configPage(){
		ModelAndView mv = new ModelAndView("notice/noticeList");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response){
		DataVector<Map<String, Object>> listData = noticeService.getList(new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) {
		String userLevel = SessionUtil.getStringAttri(request, SessionData.levelKey);
		ModelAndView mv = null;
		if (!"9".equals(userLevel)) {
			mv = new ModelAndView("common/empty");
			mv.addObject("pageInfo", "对不起，您没有此权限！");
			return mv;
		}
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/notice/noticeForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("business", "notice");
		mv.addObject("titleName", "新增公告");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("save/{action}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String action){
		String operator = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String formData = request.getParameter("formData");
		Map<String, Object> inputMap = JSONSerielizer.toMap(formData);
		
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = null;
		if ("new".equals(action)) {
			isSuccess = noticeService.add(operator, inputMap);
		}else {//
			isSuccess = noticeService.update(operator, inputMap);
		}
		map.put("isSuccess", isSuccess);
		info = isSuccess ? "操作成功。" : "操作失败！";
		map.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		Map<String, Object> formData = noticeService.getForm(id);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Boolean isSuccess = noticeService.delete(ids);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	
}
